package com.starbaba.activity.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.activity.service.ActivityLoanOrderRecordService;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.tuanzi.server.loan_base.model.db.activity.ActivityLoanOrderRecord;

public class ActivityLoanOrderRecordServiceImpl extends LoanManagerBaseDao implements ActivityLoanOrderRecordService{
	

	@Override
	public List<ActivityLoanOrderRecord> getList(Map<String, Object> paramsMap,
			int pageNo, int pageSize) {
		
		String sql = generateGetListSql(paramsMap) + " ORDER BY `ctime` DESC LIMIT " + pageNo + "," + pageSize;
		List<ActivityLoanOrderRecord> list = null;
		try {
			list = getDao().list(sql, ActivityLoanOrderRecord.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		
		return list;
	}

	@Override
	public int countList(Map<String, Object> paramsMap) {
		
		String sql = "SELECT COUNT(1) FROM ( " + generateGetListSql(paramsMap) + ") AS a";
		int result = 0;
		try {
			result = getDao().getInt(sql);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}

	@Override
	public String exportList(Map<String, Object> paramsMap) {
		
		String sql = generateGetListSql(paramsMap);
		List<ActivityLoanOrderRecord> list = new ArrayList<ActivityLoanOrderRecord>();
		try {
			list = getDao().list(sql, ActivityLoanOrderRecord.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		
		StringBuilder sb = new StringBuilder("登记时间,手机号,活动名称,登记金额\r\n");
		
		for(ActivityLoanOrderRecord record: list){
			
			String cdate =  record.getCdate();
			String mobnum = record.getMobnum();
			String activity_name = record.getActivity_name();
			double amoumt = record.getAmount();
			
			sb.append(cdate + ",")
			  .append(mobnum + ",")
			  .append(activity_name + ",")
			  .append(amoumt + "\r\n");
		}
		
		return sb.toString();
		
	}
	
	/**
	 * 生成查询列表sql
	 * @param paramsMap
	 * @return
	 */
	private String generateGetListSql(Map<String, Object> paramsMap){
		
		String startDate = (String) paramsMap.get("startDate");
		String endDate = (String) paramsMap.get("endDate");
		String activity_id = (String) paramsMap.get("activity_id");
		String mobnum = (String) paramsMap.get("mobnum");
		
		StringBuilder sql = new StringBuilder("SELECT * FROM `activity_loan_order_record` WHERE 1=1 ");
		
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" AND cdate >= '" + startDate + "'");
		}
		
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" AND cdate <= '" + endDate + "'");
		}
		
		if(StringUtils.isNotEmpty(activity_id)){
			sql.append(" AND activity_id = '" + activity_id + "'");
		}
		
		if(StringUtils.isNotEmpty(mobnum)){
			sql.append(" AND mobnum = '" + mobnum + "'");
		}
		return sql.toString();
	}

}
